#include "workqueue.h"

int enQueue(workqueue_t *q, int sockfd, train_t train){
    node_t *curNode = (node_t *)calloc(1, sizeof(node_t));
    ERROR_CHECK(curNode, NULL, "calloc");
    
    curNode->connectfd = sockfd;
    curNode->train = train;

    if(q->len == 0){
        q->head = curNode;
        q->tail = curNode;
        ++q->len;
        return 0;
    }
    q->tail->next = curNode;
    q->tail = curNode;
    ++q->len;
    return 0;
}

int deQueue(workqueue_t *q){
    if(q->len == 0){
        return 0;
    }

    node_t *curNode = q->head;
    q->head = q->head->next;
    --q->len;
    free(curNode);

    if(q->len == 0){
        q->tail = NULL;
    }
    return 0;
}

int printQueue(const workqueue_t *q){
    if(q->len == 0){
        return 0;
    }

    node_t *p = q->head;
    while(p->next != NULL){
        printf("%d --> ", p->connectfd);
        p = p->next;
    }
    printf("%d\n", p->connectfd);
    return 0;
}


// 单元测试
//int main(){
//    srand(time(NULL));
//    workqueue_t queue;
//    bzero(&queue, sizeof(queue));
//
//    for(int i = 0; i < 10; i++){
//        enQueue(&queue, rand() % 100);
//        printQueue(&queue);
//    }
//    
//    for(int i = 0; i < 10; i++){
//        deQueue(&queue);
//        printQueue(&queue);
//    }
//
//    return 1;
//}
